//Team: T102-17 
//Members: wrr3hw/ William Rinaldi; atb3df/ Austin Beane; cea7dz/ Katie Aichholz; jpp7mt/Jessica Platter

package cs2110.virginia.edu.ghosthunter;

import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Random;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.SurfaceView;
import android.view.View;
import android.graphics.*;


@SuppressLint("DrawAllocation") public class Drawer extends View {
	
//hunter
	Bitmap hunter;
	public static Hunter bill = new Hunter(20,20);
	Bitmap ghost;
	public static Ghost caspar = new Ghost(100,100, 5, 10);
	Bitmap ghost2;
	public static Ghost jerry = new Ghost (500, 500, 10, 5);
	Bitmap mine;
	Bitmap coin;
	Bitmap explosion;
	public static Mine mine1 = new Mine(20,20);
	Bitmap coinNumber1;
	public static NumberCount coinNumber = new NumberCount(10,10);
	Bitmap ghostNumber1;
	public static NumberCount ghostNumber = new NumberCount(10,10);
	Bitmap bombNumber1;
	public static NumberCount bombNumber = new NumberCount(10,10);
	
	public static Integer numberOfSuperBombs = 0;
	public static Integer numberOfGhostsLeft = 100;
	public static Integer numberOfCoins = 14;
	public static Integer numberOfLives = 5;
	public static Boolean billIsDead = false;
	
	
//	protected int coinNumber2 = 0;
//	private int renderedScore;
//	private String renderedScoreString;
//	Paint FILL;
//	Paint paintObj = new Paint();
//	paintObj.setTextSize(textSize);
//	paintObj.setColor(Color.WHITE);
//	
//	public void draw(Canvas c) {
//	    if (this.coinNumber2 != this.renderedScore || this.renderedScoreString == null) {
//	        this.renderedScore = this.coinNumber2;
//	        this.renderedScoreString = Integer.toString(this.renderedScore);
//	        c.drawText(Integer.toString(this.renderedScore), 200, 300, FILL);
//	    }
//	    
//	}
	
	
	
	
//	public void render(Canvas canvas){
//	    Bitmap bitmap;
//	    Graphics.Coordinate coords;
//	    canvas.drawBitmap(bgBitmap, 0, 0, null);
//	    canvas.drawBitmap(closeBtnBitmap, 700, 0, null);
//	    canvas.drawBitmap(groundBitmap, 0, 315, null);
//	    canvas.drawBitmap(petBitmap, petX, petY, null);
//	    for(Graphics pics : coins){
//	        bitmap = pics.getBitmap();
//	        coords = pics.getCoord();
//	        canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
//	    }
//	    canvas.drawBitmap(scoreBitmap, 300, 20, null);
//	    canvas.drawText(scoreString, 300, 20, null); //change null to paintObj
//	}
//	
//	private void updateScore(int score){
//	    initScore += score;
//	    totalScore = initScore;
//	    scoreString = Integer.toString(totalScore);
//	}
	
	//Edit: Solved, NullPointerException caused by null paint object. Simply create paint object Paint paintObj = new Paint(); and set the object paintObj.setTextSize(textSize) and paintObj.setColor(Color.WHITE);
	
	
	public Drawer(Context c) {
		//hunter
		super(c);
		hunter = BitmapFactory.decodeResource(getResources(), R.drawable.characterfrontlookingleft);
		ghost = BitmapFactory.decodeResource(getResources(), R.drawable.ghost1);
		ghost2 = BitmapFactory.decodeResource(getResources(), R.drawable.ghost2);
		mine = BitmapFactory.decodeResource(getResources(), R.drawable.mineweapon);
		coin = BitmapFactory.decodeResource(getResources(), R.drawable.super_mario_coin);
		explosion = BitmapFactory.decodeResource(getResources(), R.drawable.boom);


//		if (System.currentTimeMillis()==2000){
//			ghostlist.add(new Ghost(100,100,5,10));
//			count++;
			
		//}
	}

	public Drawer(Context c, AttributeSet a, int d) {
		super(c, a, d);
		hunter = BitmapFactory.decodeResource(getResources(), R.drawable.characterfrontlookingleft);
		ghost = BitmapFactory.decodeResource(getResources(), R.drawable.ghost1);
		ghost2 = BitmapFactory.decodeResource(getResources(), R.drawable.ghost2);
		mine = BitmapFactory.decodeResource(getResources(), R.drawable.mineweapon);
		coin = BitmapFactory.decodeResource(getResources(), R.drawable.super_mario_coin);
		explosion = BitmapFactory.decodeResource(getResources(), R.drawable.boom);


//		if (System.currentTimeMillis()==2000){
//			ghostlist.add(new Ghost(100,100,5,10));
//			count++;
//			
//		}

	}

	public Drawer(Context c, AttributeSet a) {
		super(c, a);
		hunter = BitmapFactory.decodeResource(getResources(), R.drawable.characterfrontlookingleft);
		ghost = BitmapFactory.decodeResource(getResources(), R.drawable.ghost1);
		ghost2 = BitmapFactory.decodeResource(getResources(), R.drawable.ghost2);
		mine = BitmapFactory.decodeResource(getResources(), R.drawable.mineweapon);
		coin = BitmapFactory.decodeResource(getResources(), R.drawable.super_mario_coin);
		explosion = BitmapFactory.decodeResource(getResources(), R.drawable.boom);


		
//		if (System.currentTimeMillis()==2000){
//			ghostlist.add(new Ghost(100,100,5,10));
//			count++;
//			
//		}

	}
		public static ArrayList<Ghost> ghostlist = new ArrayList<Ghost>();
		int count = 0;
		public static ArrayList<Mine> minelist = new ArrayList<Mine>();
		public static ArrayList<Coin> coinlist = new ArrayList<Coin>();
		

	public void lister() throws InterruptedException{
	
//		int count = 0;
//		while (count < 5) {
//			ghostlist.add(new Ghost(0, 0, 5, 10));
//			count++;
//			wait(5000);
//		}
		}

	public static void mineSound() {
		Thread playMineExplosion = new Thread() {
			public void run() {
				MediaPlayer mineSound;
				mineSound = MediaPlayer.create(null, R.raw.basicbombsound);
				mineSound.start();
			}
		};
		playMineExplosion.run();
		
	}
	
	
	public void onDraw(Canvas c){
		super.onDraw(c);
		
		if (numberOfLives > 0) {
		
		if (count%60==0){
			ghostlist.add(new Ghost((int)(800*Math.random()+1),(int)(900*Math.random()+1),5,10));
			
		}
		try {
		for (int i=0; i<ghostlist.size(); i++){
			Ghost tempGhost = ghostlist.get(i);
			
			if ((Math.abs((bill.getx() + 100)-(tempGhost.getx() + 150))<=230)&&(Math.abs((bill.gety() + 100)-(tempGhost.gety()+ 150))<=315)) {
				Paint paint = new Paint(); 
				paint.setColor(Color.RED);
				paint.setTextSize(25); 
				c.drawText( "INCOMING GHOST!!!!", 3, 200, paint);
				}
			
			
			for (Mine m: minelist){
				c.drawBitmap(mine, m.getx(), m.gety(), new Paint());
				if ((Math.abs((tempGhost.getx() + 100)-(m.getx() + 50))<=100)&&(Math.abs((tempGhost.gety() + 50)-(m.gety()) + 50)<=100)){
					//mineSound();
					ghostlist.remove(tempGhost);
					numberOfGhostsLeft--;
					minelist.remove(m);
					c.drawBitmap(explosion, tempGhost.getx(), tempGhost.gety(), new Paint());
					c.drawBitmap(explosion, tempGhost.getx(), tempGhost.gety(), new Paint());
					c.drawBitmap(explosion, tempGhost.getx(), tempGhost.gety(), new Paint());
					c.drawBitmap(explosion, tempGhost.getx(), tempGhost.gety(), new Paint());
					c.drawBitmap(explosion, tempGhost.getx(), tempGhost.gety(), new Paint());
					c.drawBitmap(explosion, tempGhost.getx(), tempGhost.gety(), new Paint());

					coinlist.add(new Coin(tempGhost.getx(), tempGhost.gety()));

				}

			}
			
			tempGhost.setx(tempGhost.getx()+tempGhost.getvx());
			tempGhost.sety(tempGhost.gety()+tempGhost.getvy());
			c.drawBitmap(ghost, tempGhost.getx(), tempGhost.gety(), new Paint());
		}
		for (Coin k: coinlist){
			c.drawBitmap(coin, k.getx(), k.gety(), new Paint() );
			if ((Math.abs(k.getx()-Drawer.bill.getx())<=60)&&(Math.abs(k.gety()-Drawer.bill.gety())<=100)){
				coinlist.remove(k);
				numberOfCoins++;
			}
		}
		
		for (Ghost g: ghostlist){
			c.drawBitmap(ghost, g.getx(), g.gety(), new Paint());
			if ((Math.abs((bill.getx() + 100)-(g.getx() + 150))<=100)&&(Math.abs((bill.gety() + 100)-(g.gety()+ 150))<=150)){
			//mineSound();
			ghostlist.removeAll(ghostlist);
			minelist.removeAll(minelist);
			bill.setx(50);
			bill.sety(50);
			numberOfLives--;

			}
		}
		
	}

		catch (ConcurrentModificationException thisStupidStuff) {
			
		}
		
		
// 		Thread dropTheMine = new Thread() {
//			public void run() {
//	    		try { 
//	    			for (Mine m: minelist){
//	    				c.drawBitmap(mine, m.getx(), m.gety(), new Paint());
//	    				for (Ghost each: ghostlist){
//	    					if ((Math.abs(each.getx()-m.getx())<=20)&&(Math.abs(each.gety()-m.gety())<=20)){
//	    						ghostlist.remove(each);
//	    					}
//	    					
//	    				}
//
//	    				
//	    			}
//	    			
//	    		}
//	    		
//	    		catch (NullPointerException somethingIsWrong) {
//	    			
//	    		}
//	    		
//	    		finally {
//	    			
//	    		}
//	    	}
//	    };
	    
	    //dropTheMine.run();
		
		
//		caspar.setx(caspar.getx()+caspar.getvx());
//		caspar.sety(caspar.gety()+caspar.getvy());
//		
//		jerry.setx(jerry.getx()+jerry.getvx());
//		jerry.sety(jerry.gety()+jerry.getvy());
		
		c.drawBitmap(hunter, bill.getx(), bill.gety(), new Paint());
//		c.drawBitmap(ghost, caspar.getx(), caspar.gety(), new Paint());
//		c.drawBitmap(ghost2, jerry.getx(), jerry.gety(), new Paint());
//		c.drawBitmap(ghost, new Ghost(100,100, 5, 10).getx(), new Ghost(100,100, 5, 10).gety(), new Paint());
		
//		if(jerry.getx()-Drawer.bill.getx()<=10&&jerry.gety()-Drawer.bill.gety()<=10){
//			ghost2.recycle();
//		}
		count++;
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				invalidate(); 
			}
			
		}, 100); 
		
		Paint paint = new Paint(); 
		paint.setColor(Color.RED); 
		paint.setTextSize(50); 
		c.drawText( "Ghosts Left: " + numberOfGhostsLeft, 385, 50, paint);
		c.drawText( "Bombs: " + numberOfSuperBombs, 385, 110, paint);
		c.drawText( "Coins: " + numberOfCoins, 385, 170, paint);
		c.drawText( "Lives: " + numberOfLives, 385, 230, paint);
		
		if (numberOfCoins >= 15) {
		paint.setColor(Color.RED);
		paint.setTextSize(25); 
		c.drawText( "SUPER BOMB AVAILABLE!!!", 3, 150, paint);
		}

		}
		
		else {
			Paint paint = new Paint(); 
			paint.setColor(Color.RED);
			paint.setTextSize(115); 
			c.drawText("GAME OVER!!!", 25, 450, paint);
			paint.setTextSize(30); 
			c.drawText("Press the back button to stop app and reset", 75, 550, paint);
		}
		
	}
}
